//
//  GSecondLevelView.m
//  helpinrain
//
//  Created by Gang.Wang on 12-7-26.
//  Copyright (c) 2012年 Zhuhai Yuike Technology Co., Ltd. All rights reserved.
//

#import "GSecondLevelView.h"
#import "mcf.h"
#import "GSheetXmlModel.h"
#import "GSecondViewItemView.h"

#define COLUMNS                             2

@interface GSecondLevelView ()
{
    UIScrollView * contentScrollView_;
    NSMutableDictionary * itemViewDic_;
    
    UIButton * backButton_;
    
}
@property (nonatomic, strong) UIScrollView * contentScrollView;
@property (nonatomic, strong) NSMutableDictionary * itemViewDic;

@property (nonatomic, strong) UIButton * backButton;

- (void) privateInit;

@end

@implementation GSecondLevelView

@synthesize delegate = delegate_;
@synthesize contentScrollView = contentScrollView_;

@synthesize sheet = sheet_;
@synthesize itemViewDic = itemViewDic_;

@synthesize backButton = backButton_;

- (void)dealloc
{
    YRelease(contentScrollView_)
    YRelease(sheet_)
    
    YRelease(itemViewDic_)
    YRelease(backButton_)
    
    [super dealloc];
}


- (id) init
{
    self = [super init];
    if (self) 
    {
        [self privateInit];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
         [self privateInit];
    }
    return self;
}

- (void) privateInit
{
    if (self.bInited) 
    {
        return;
    }
    
    [self setBackgroundColor:RGB(74, 82, 89)];
    
    UIScrollView * temp_scrollview = [[UIScrollView alloc] init];
    self.contentScrollView = temp_scrollview;
    YRelease(temp_scrollview)
    [self addSubview:self.contentScrollView];
    
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self addSubview:self.backButton];
    BUTTON_FUNCTION(self.backButton, back)
    
    [self reAdjustLayout];
    self.bInited = TRUE;
}


- (void) back
{
    DELEGATE_CALLBACK(closeSecondLevelView:, self)
}

- (void) generateNilDataAppInfoViews
{
    if (!self.itemViewDic) 
    {
        NSMutableDictionary * temp_dic = [[NSMutableDictionary alloc] init];
        self.itemViewDic = temp_dic;
        YRelease(temp_dic)
    }
    
    
    float originYFlag = 0;
    //    int canShowTopLineCount = COLUMNS;
    //    BOOL showRightLine = FALSE;
    for (int i= 0; i< [self.sheet.magazine.sections count]; i++) 
    {
        CGRect frame = CGRectZero;
        
        if (i % 2 != 0) 
        {
            frame.origin.x  = (self.contentScrollView.frame.size.width / COLUMNS);
        }
        else 
        {            
            frame.origin.x = 0;
        }
        
        frame.origin.y = originYFlag;
        frame.size.width = self.contentScrollView.frame.size.width / COLUMNS;
        frame.size.height = self.contentScrollView.frame.size.width / COLUMNS;
        
        int heightFlag = i;
        if (heightFlag % 2 != 0) 
        {
            originYFlag += self.contentScrollView.frame.size.width / COLUMNS;
        }
        
        GSecondViewItemView * addedView = [self.itemViewDic objectForKey:[NSString stringWithFormat:@"%d", i]];
        
        if (addedView) 
        {
            [addedView setFrame:frame];
            continue;
        }
        
        GSecondViewItemView * appView = [[GSecondViewItemView alloc] init];
        [appView setDelegate:self];
        [appView setFrame:frame];  
//        [appView reAdjustLayout];
         
         /* if ( canShowTopLineCount-- > 0) 
         {
         [appView showTopLine];
         
         [appView showBottomLine];
         
         }
         else 
         {
         [appView showBottomLine];
         }
         
         showRightLine = !showRightLine;
         if (showRightLine) 
         {
         [appView showRightLine];
         }
         
         */
        
        [self.itemViewDic setObject:appView forKey:[NSString stringWithFormat:@"%d", i]];
        [self.contentScrollView addSubview:appView];
        YRelease(appView)
        
        
    }
    

}

- (void) reAdjustLayout
{
    
    [self.backButton setFrame:CGRectMake(10, 10, isPad ? 40 : 30, isPad ? 40 : 30)];
    
    if (currrentInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
        currrentInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    {
        [self.contentScrollView setFrame:CGRectMake(isPad ? 150 : 30, 20, self.width - (isPad ? 300 : 60), self.height - 40)];
    }
    else 
    {
        [self.contentScrollView setFrame:CGRectMake(isPad ? 100 : 30, 20, self.width - (isPad ? 200 : 60), self.height - 40)];
    }
    
    
    
    int rowCount = [self.itemViewDic count];
    if (rowCount % 2 != 0) 
    {
        rowCount++;
    }
    
    rowCount = rowCount / 2;
    
    [self.contentScrollView setContentSize:CGSizeMake(self.contentScrollView.frame.size.width, rowCount * self.contentScrollView.frame.size.width / COLUMNS)];
    [self generateNilDataAppInfoViews];
}



- (void) showData2View
{
    if (!self.sheet) 
    {
        return;
    }
    
    int index = 0;
    for(GSection * section in self.sheet.magazine.sections)
    {
        if (!section) 
        {
            continue;
        }
        
        GSecondViewItemView * itemView = (GSecondViewItemView *) [self.itemViewDic objectForKey:[NSString stringWithFormat:@"%d", index++]];
        if (!itemView) 
        {
            continue;
        }
        
        [itemView setSection:section];        
    } 
   
}


- (void) setSheet:(GSheetXmlModel *)sheetValue
{
    YRelease(sheet_)
    sheet_ = [sheetValue retain];
    
    if (!sheetValue) 
    {
        return;
    }
    
    [self generateNilDataAppInfoViews];
    [self showData2View];
    
    [self reAdjustLayout];
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
//    [self reAdjustLayout];
}


- (void) openSection:(id)section
{
    if (!section) 
    {
        return;
    }
    
    DELEGATE_CALLBACK(openSection:, section)
}





@end
